package cn.lanqiao.bigDate.service.data.impl;


import cn.lanqiao.bigDate.entity.data.DataBook;
import cn.lanqiao.bigDate.mapper.data.DataBookMapper;
import cn.lanqiao.bigDate.service.data.DataBookService;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

// 这谁写的？还有一个是在DataBookServiceImpl
@Service
public class DataBookImpl implements DataBookService {

    @Resource
    private DataBookMapper dataBookMapper;

    /**
     * 分页
     * @param page
     * @param searchBookFraming
     * @param searchBookFormat
     * @param searchBookNameAndIsbn
     * @return
     */
    @Override
    public IPage<DataBook> findBookByPage(Page<DataBook> page,String searchBookNameAndIsbn ,String searchBookFormat,String searchBookFraming ){
        QueryWrapper<DataBook> wrapper = new QueryWrapper<>();
        if (!searchBookFormat.equals("")){
            wrapper.eq("format",searchBookFormat);
        }
        if (!searchBookFraming.equals("")){
            wrapper.eq("framing", searchBookFraming);
        }
        if (!searchBookNameAndIsbn.equals("")){
            wrapper.like("name",searchBookNameAndIsbn).or().like("isbn",searchBookNameAndIsbn);
        }
        Page<DataBook> dataBookPage = dataBookMapper.selectPage(page,wrapper);
        return dataBookPage;
    }
    /**
     * 按id查询
     * @param id
     * @return
     */
    @Override
    public DataBook findBookOne(Long id) {
        QueryWrapper<DataBook> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        DataBook dataBook = dataBookMapper.selectOne(wrapper);
        return dataBook;
    }
    /**
     * 按name
     * @param name
     * @return
     */
    @Override
    public List<String> findBookName(String name) {
        List<String> dataBook = dataBookMapper.findBookName(name);
        return dataBook;
    }

    /**
     * 按id删除
     * @param ids
     * @return
     */
    @Override
    public Boolean deleteBookById(String ids) {
        if(!StringUtils.isEmpty(ids)){
            String[] id = ids.split(",");
            List<String> idList = Arrays.asList(id);
            int i = dataBookMapper.deleteBatchIds(idList);
            return i > 0;
        }
        return false;
    }
    /**
     *添加图书信息
     * @param dataBook
     * @return
     */
    @Override
    public Boolean addBookData(DataBook dataBook) {
        if (dataBook.getName()!=null&& dataBook.getIsbn()!=null&& dataBook.getDate()!=null&& dataBook.getAuthorId()!=null&& dataBook.getPbhouseId()!=null&& dataBook.getFormat()!=null&& dataBook.getFraming()!=null&& dataBook.getPrice()!=null){
            dataBook.setCreateTime(LocalDateTime.now());
            int insert = dataBookMapper.insert(dataBook);
            if (insert == 1){
                return true;
            }
        }
        return false;
    }

    /**
     * 修改图书信息
     * @param dataBook
     * @return
     */
    @Override
    public Boolean updateBookById(DataBook dataBook) {
        if (dataBook.getName()!=null&& dataBook.getIsbn()!=null&& dataBook.getDate()!=null&& dataBook.getAuthorId()!=null&& dataBook.getPbhouseId()!=null&& dataBook.getFormat()!=null&& dataBook.getFraming()!=null&& dataBook.getPrice()!=null && dataBook.getId()!=null){
            Long id = dataBook.getId();
            DataBook dataBookOne = findBookOne(id);
            dataBookOne.setName(dataBook.getName());
            dataBookOne.setImg(dataBook.getImg());
            dataBookOne.setAuthorId(dataBook.getAuthorId());
            dataBookOne.setPbhouseId(dataBook.getPbhouseId());
            dataBookOne.setDate(dataBook.getDate());
            dataBookOne.setPrice(dataBook.getPrice());
            dataBookOne.setFraming(dataBook.getFraming());
            dataBookOne.setFormat(dataBook.getFormat());
            dataBookOne.setIsbn(dataBook.getIsbn());
            dataBookOne.setUpdateTime(LocalDateTime.now());
            int i = dataBookMapper.updateById(dataBookOne);
            if (i==1){
                return true;
            }
            return false;
        }
        return false;
    }

    /**
     * 查找书名
     * @param name
     * @return
     */
    @Override
    public Boolean findBookByName(String name){
        QueryWrapper<DataBook> wrapper = new QueryWrapper<>();
        wrapper.eq("name",name);
        List<DataBook> dataBook = dataBookMapper.selectList(wrapper);
        if (dataBook.size()==0){
            return true;
        }else {
            return false;
        }
    }
    /**
     * 查找书名
     * @param isbn
     * @return
     */
    @Override
    public Boolean findBookByIsbn(String isbn){
        QueryWrapper<DataBook> wrapper = new QueryWrapper<>();
        wrapper.eq("isbn",isbn);
        List<DataBook> dataBook = dataBookMapper.selectList(wrapper);
        if (dataBook.size()==0){
            return true;
        }else {
            return false;
        }
    }
}
